﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;

namespace YJKArch.WallModule
{
	public partial class EditWallThicknessForm : System.Windows.Forms.Form
	{
		public EditWallThicknessForm(WallKind wallKind, string refWallTypeName, List<WallThicknessInfos> wallThicknessInfos, List<string> allWallTypeNames)
		{
			this.InitializeComponent();
			this.m_WallKind = wallKind;
			this.m_AllWallTypeNames = allWallTypeNames;
			this.m_WallThicknessInfos = wallThicknessInfos;
			this.InitDGV();
			this.tbTypeName.Text = "HY_" + refWallTypeName;
		}

		public string NewWallTypeName
		{
			get
			{
				return this.tbTypeName.Text;
			}
		}

		public List<WallThicknessInfos> WallThicknessInfos
		{
			get
			{
				return this.m_WallThicknessInfos;
			}
		}

		private void DGVWallThickness_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
			if (textBox != null)
			{
				textBox.TextChanged += this.DGVTextBoxTextChanged;
			}
		}

		private void InitDGV()
		{
			this.DGVWallThickness.Rows.Clear();
			if (this.m_WallThicknessInfos.Count > 0)
			{
				this.DGVWallThickness.RowCount = this.m_WallThicknessInfos.Count;
				for (int i = 0; i < this.m_WallThicknessInfos.Count; i++)
				{
					this.DGVWallThickness.Rows[i].Cells[0].Value = this.m_WallThicknessInfos[i].m_Name;
					double width = this.m_WallThicknessInfos[i].m_Width;
					this.DGVWallThickness.Rows[i].Cells[1].Value = width.ToString();
					if (this.m_WallKind != null)
					{
						this.DGVWallThickness.Rows[i].ReadOnly = true;
						this.DGVWallThickness.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
						this.DGVWallThickness.Rows[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Black;
					}
				}
			}
		}

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			int num = 1;
			if (this.DGVWallThickness.CurrentCell.ColumnIndex == num)
			{
				int rowIndex = this.DGVWallThickness.CurrentCell.RowIndex;
				string text = ((System.Windows.Forms.TextBox)sender).Text;
				int num2 = 0;
				try
				{
					num2 = Convert.ToInt32(text);
				}
				catch (Exception)
				{
					((System.Windows.Forms.TextBox)sender).Text = this.m_WallThicknessInfos[rowIndex].m_Width.ToString();
					return;
				}
				if (num2 < 0)
				{
					((System.Windows.Forms.TextBox)sender).Text = this.m_WallThicknessInfos[rowIndex].m_Width.ToString();
					MessageBox.Show("宽度不能小于零", "盈建科提示");
					return;
				}
				this.m_WallThicknessInfos[rowIndex] = new WallThicknessInfos(this.m_WallThicknessInfos[rowIndex].m_Funtion, this.m_WallThicknessInfos[rowIndex].m_Name, (double)num2);
			}
		}

		private bool HasSpecialChar(string str)
		{
			char[] separator = new char[]
			{
				'/',
				'\\',
				':',
				'*',
				'?',
				'"',
				'<',
				'>',
				'|'
			};
			return str.Split(separator).Length != 1;
		}

		private void EditWallThicknessForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.Cancel)
			{
				return;
			}
			string newTypeName = this.tbTypeName.Text;
			if (newTypeName.CompareTo("") == 0)
			{
				MessageBox.Show("墙类型名不能为空", "盈建科提示");
				e.Cancel = true;
			}
			if (this.m_AllWallTypeNames.FindIndex((string t) => t.CompareTo(newTypeName) == 0) >= 0)
			{
				MessageBox.Show("墙类型名已存在", "盈建科提示");
				e.Cancel = true;
			}
			if (this.HasSpecialChar(newTypeName))
			{
				MessageBox.Show("墙类型名含有非法字符", "盈建科提示");
				e.Cancel = true;
			}
			foreach (WallThicknessInfos wallThicknessInfos in this.m_WallThicknessInfos)
			{
				if (wallThicknessInfos.m_Name.CompareTo("涂膜层") == 0 && !this.IsEqual(wallThicknessInfos.m_Width, 0.0))
				{
					MessageBox.Show("\"涂膜层\"功能要求厚度为零", "盈建科提示");
					e.Cancel = true;
					break;
				}
			}
		}

		private bool LessThan(double val1, double val2)
		{
			return val1 - val2 < -1E-09;
		}

		private bool IsEqual(double val1, double val2)
		{
			return !this.LessThan(val1, val2) && !this.LessThan(val2, val1);
		}

		private List<string> m_AllWallTypeNames = new List<string>();

		private List<WallThicknessInfos> m_WallThicknessInfos = new List<WallThicknessInfos>();

		private WallKind m_WallKind;
	}
}
